package com.zhiche.lisa.lspm.service.sys.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.lspm.dao.mapper.sys.UserRoleMapper;
import com.zhiche.lisa.lspm.dao.model.sys.UserRole;
import com.zhiche.lisa.lspm.service.driver.IDriverService;
import com.zhiche.lisa.lspm.service.fleet.FleetAdminService;
import com.zhiche.lisa.lspm.service.lsp.LspInfoService;
import com.zhiche.lisa.lspm.service.sys.IUserRoleService;
import com.zhiche.lisa.lspm.vo.sys.RoleUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yangzhiyu
 * @since 2018-09-28
 */
@Service
public class UserRoleServiceDiy extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    LspInfoService lspInfoService;

    @Autowired
    IDriverService driverService;

    @Autowired
    FleetAdminService fleetAdminService;


    @Override
    public void addAndUpdata(UserRole userRole) {
        if (Objects.isNull(userRole.getUserId())) {
            throw new BaseException("授权失败,请联系管理人员!");
        }
        if (Objects.isNull(userRole.getId())) {
            this.insert(userRole);
        } else {
            this.updateById(userRole);
        }
    }

    @Override
    public Page<RoleUserVO> queryRolePermission(Page<RoleUserVO> page) {
        Wrapper<RoleUserVO> roleUserVOWrapper = new EntityWrapper<>();
        if (page == null) {
            throw new BaseException("page 参数不能为空");
        }
        if (!StringUtils.isEmpty(String.valueOf(page.getCondition().get("mobile"))))
            roleUserVOWrapper.like("su.mobile", page.getCondition().get("mobile").toString());
        if (!StringUtils.isEmpty(String.valueOf(page.getCondition().get("name"))))
            roleUserVOWrapper.like("su.name", page.getCondition().get("name").toString());
        roleUserVOWrapper.orderBy("su.id", false);
        List<RoleUserVO> roleUserVOS = baseMapper.queryRolePermission(page, roleUserVOWrapper);
        return page.setRecords(roleUserVOS);
    }
}
